<template>
  <div class="opera-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Search" plain type="primary" @click="openQueryDialog">
          查询
        </el-button>
        <el-button :icon="DocumentAdd" plain type="primary" @click="openAdd">
          新增
        </el-button>
        <el-button :icon="Edit" plain type="warning" @click="openEdit">
          编辑图片
        </el-button>
        <el-button :icon="Delete" plain type="danger" @click="deleteGytz">
          删除
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      :cell-style="{ padding: '4px 0' }"
      :data="state.gytzData"
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <el-table-column label="序号" type="index" width="80" />
      <el-table-column label="编码" prop="gytzCode" width="200" />
      <el-table-column label="名称" prop="gytzName" width="220" />
      <el-table-column label="备注" prop="remark" width="220" />
      <el-table-column label="创建信息" width="200">
        <template #default="scope">
          <el-row>
            <span style="font-size: 16px; font-weight: 700; color: #000">
              {{ scope.row.createUser }}
            </span>
          </el-row>
          <el-row>
            <span>
              {{ scope.row.createTime }}
            </span>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column label="修改信息" width="200">
        <template #default="scope">
          <el-row>
            <span style="font-size: 16px; font-weight: 700; color: #000">
              {{ scope.row.updateUser }}
            </span>
          </el-row>
          <el-row>
            <span>
              {{ scope.row.updateTime }}
            </span>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column label="工艺图纸">
        <template #default="scope">
          <el-button link type="primary" @click="handleClick">
            {{ scope.row.fileUrl }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="paginationParams.pageNum"
      :layout="layout"
      :page-size="paginationParams.pageSize"
      :total="paginationParams.total"
      @current-change="handlePaginationCurrentChange"
      @size-change="handlePaginationSizeChange"
    />
    <GytzModuleVue ref="gytzModuleRef" @save-success="getGytz" />
    <QueryDialogVue ref="queryDialogRef" @query-callback="queryCallback" />
    <InvSelectVue ref="invSelectRef" @selected-callback="invSelectCallback" />
  </div>
</template>

<script setup>
  import { DocumentAdd, Search, Edit, Delete } from '@element-plus/icons-vue'
  import { getGytzPageApi, addGytzApi } from '@/api/mes/gytz_api'
  import { localStorageConfig } from '@/config/localstorage.config'
  import GytzModuleVue from './modules/gytzModule.vue'
  import QueryDialogVue from './modules/queryDialog.vue'
  import InvSelectVue from '@/plugins/InventorySelector/invSelect.vue'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)

  const state = reactive({
    gytzData: [],
    currentRow: {},
  })
  const queryForm = ref({ pkCorp: pkCorp })

  const paginationParams = reactive({
    pageNum: 1,
    pageSize: 20,
    total: 0,
  })

  const gytzModuleRef = ref()
  const queryDialogRef = ref()
  const invSelectRef = ref()

  const openQueryDialog = () => {
    queryDialogRef.value.openDialog()
  }
  const queryCallback = (value) => {
    queryForm.value = value
    getGytz()
  }

  const getGytz = async () => {
    const load = $baseLoading()
    try {
      let params = {
        ...queryForm.value,
        ...paginationParams,
      }
      const res = await getGytzPageApi(params)
      if (res.code === 2000) {
        $baseMessage(res.msg, 'success')
        state.gytzData = res.data.list
        paginationParams.total = res.data.total
      } else {
        $baseMessage(res.msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  const openAdd = () => {
    invSelectRef.value.handleOpen({}, 0)
    // gytzModuleRef.value.handleOpen()
  }
  const invSelectCallback = async (inventory) => {
    console.log(inventory)
    let sendData = {
      pkCorp: pkCorp,
      pkInv: inventory.pkInv,
      gytzCode: inventory.invcode,
      gytzName: inventory.invname,
    }
    const { code, msg } = await addGytzApi(sendData)
    if (2000 === code) {
      $baseMessage(msg, 'success')
    } else {
      $baseMessage(msg, 'error')
    }
    getGytz()
  }
  const handleCurrentChange = (row) => {
    state.currentRow = row
  }
  const openEdit = () => {
    if (state.currentRow.pkMesGytz === undefined) {
      $baseMessage('请选择需要编辑图片的数据', 'warning')
    } else {
      gytzModuleRef.value.handleOpen(state.currentRow)
    }
  }
  const deleteGytz = () => {}

  //#region pagintion 开始
  const layout = ref('total, sizes, prev, pager, next, jumper')
  const handlePaginationCurrentChange = (...args) => {
    paginationParams.pageNum = args[0] ? args[0] : 1
  }
  const handlePaginationSizeChange = (...args) => {
    paginationParams.pageSize = args[0] ? args[0] : 20
    paginationParams.pageNum = 1
  }
  //#endregion

  watch(paginationParams, () => getGytz())

  onMounted(() => getGytz())
</script>
